import pyglet
from pyglet.gl import *
import cocos
from cocos.director import director
from cocos.layer import Layer, ColorLayer
from cocos.scene import Scene
from cocos.actions import Delay, CallFunc, FadeTo, FadeOut, Show, Hide, FadeIn, FadeOut
from cocos.text import Label
from cocos.sprite import Sprite

import audio
import gg

# splash scene
# responsabilities:
#   entertain the load time
#   precache some resources
#   call audio initialization
# module must register the scene creator

class SplashLayer(Layer):
    is_event_handler = True

    def __init__(self, size=None):
        super(SplashLayer, self).__init__()
        if size == None:
            size = director.get_window_size()
        w,h = size
##        self.w = size[0]
##        self.h = size[1]
        self.done = False
        self.stage = 0
        self.it = None # steper for the current init stage
        self.set_up()
        self.schedule(self.update)

    def set_up(self):
        # estado inicial de la escena
        bg = ColorLayer(40,225,40,255)
        self.add(bg)
        w,h = director.get_window_size()
        bg.add(Label("hello", (w/2, h/2),font_name='AXAXAX.TTF',font_size=36,
                     color=(0,0,0,255)))


    def animate(self,dt):
        pass

    def update(self,dt):
        # animar la escena, lightweight
        self.animate(dt)
        # secuenciar las otras tareas: audio init, precache
        if self.it is None:
            try:
                self.it = getattr(self, 'init_%d'%self.stage)()
            except AttributeError:
                self.done = True
                print 'init done'
        else:
            try:
                self.it.next()
            except StopIteration:
                self.it = None
                self.stage += 1
        #if self.done:
            

    def init_0(self):
        # init audio
        it = audio.init()
        return it

# services
def get_splash_scene():
    layer = SplashLayer()
    scene = Scene()
    scene.add(layer)
    return scene

# publish services
gg.services["s_get_splash_scene"] = get_splash_scene
